


const express = require('express')
const bodyParser = require('body-parser')
const path = require('path')
const cors = require('cors')
const app = express()
// 使用cors 中间件，解决跨域问题
// app.use(cors())
app.use(bodyParser.urlencoded({extended:false}))
app.use(bodyParser.json())

app.use(express.static(path.join(__dirname,'./public')))
var whitelist = ['http://127.0.0.1:5500', 'http://192.168.12.34:3000']
// cors({origin:whitelist,methods:"GET,HEAD,PUT,PATCH,POST,DELETE"})
app.get('/gettime',(req,res)=>{
    res.send(JSON.stringify({
        err_code:0,
        result:{
            time:'2020-10-30 08:58:48'
        },
        msg:'请求成功'
    }))
})
app.post('/gettime',(req,res)=>{
    res.send(JSON.stringify({
        err_code:0,
        result:{
            time:'2020-10-30 08:58:48'
        },
        msg:'请求成功'
    }))
})
// get有参
app.get('/getstudent',(req,res)=>{
    let id = req.query.id
    console.log(req.query);
    console.log(req.params);
    console.log(`id=${id}`);
    // 去数据库查询
    res.send(JSON.stringify({
        err_code:0,
        result:{
           stu:{
               name:'lisi',
               age:18,
               gender:1
           }
        },
        msg:'请求成功'
    }))
})
app.post('/getstudent',(req,res)=>{
   console.log(req.body);
    // 去数据库查询
    res.send(JSON.stringify({
        err_code:0,
        result:{
           stu:{
               name:'lisi',
               age:18,
               gender:1
           }
        },
        msg:'请求成功'
    }))
})



app.listen(3000,()=>{
    console.log(`服务已启动...`);
})